<?php
class Util {

    /**
     *  Inserts a string into an other in the specified position
     *
     *  @param  string  $insertstring   the string you want to insert
     *  @param  string  $intostring     the string you want to insert it into
     *  @param  int     $offset         the offset
     *
     *  @return string the new string with the $insertingstring insertted into $intostring at the position $offset
     */
    static public function str_insert($insertstring, $intostring, $offset) {
        $part1 = substr($intostring, 0, $offset);
        $part2 = substr($intostring, $offset);

        $part1 = $part1 . $insertstring;
        $whole = $part1 . $part2;
        
        return $whole;
    }

    /**
     *  Given an image file (.jpg), creates a thumbnail (.jpg) according to the specified width and height.
     *  The thumbnail will be stored in the same folder as the original image. If the file image width o height
     *  aren't bigger than the specified width and height for creating the thumbnail no thumbnail will be created.
     *
     *  @param  string  $originalImage  Original image file path, must be a .jpg image.
     *  @param  int     $toWidth        The width for the thumbnail
     *  @param  int     $toHeight       The height for the thumnail
     *  @param  string  $thumbnailName  Optional, the name used for the new thumbnail .jpg file, must not contain '.' or '/'.
     *
     *  @return string  name of the thumbnail file
     */
    static public function resizeImage($originalImage, $toWidth, $toHeight, $thumbnailName = '') {
        list($width, $height) = getimagesize($originalImage);

        if(!($width > $toWidth) && !($height > $toHeight)){
            $path_parts = pathinfo($originalImage);
            return $path_parts['basename'];
        } else {
            $xscale=$width/$toWidth;
            $yscale=$height/$toHeight;

            if ($yscale>$xscale){
                $new_width = round($width * (1/$yscale));
                $new_height = round($height * (1/$yscale));
            }
            else {
                $new_width = round($width * (1/$xscale));
                $new_height = round($height * (1/$xscale));
            }


            $imageResized = imagecreatetruecolor($new_width, $new_height);
            $imageTmp     = imagecreatefromjpeg ($originalImage);
            imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

            $thumb = '';

            if(strcmp($thumbnailName, '') == 0){
                $ext = strrchr($originalImage, '.');
                $path_parts = pathinfo($originalImage);
                $thumb = sha1($path_parts['filename'].'_thumbnail'.rand(11111, 99999)).'.jpg';
                $thumbFile = $path_parts['dirname'].'/'.$thumb;
            } else {
                $path_parts = pathinfo($originalImage);

                $thumb = $thumbnailName.'.jpg';
                
                $thumbFile = $path_parts['dirname'].$thumb;
            }

            imagejpeg($imageResized, $thumbFile);

            imagedestroy($imageResized);

            return $thumb;
        }
    }

    static public function saveTempFile($file, $id = false) {
        // Where the file is going to be placed
        $target_path = sfConfig::get('sf_upload_dir').($id ? '/portal_'.$id.'/temp_files/':'/temp_files/');

        /* Add the original filename to our target path.
        Result is "path/filename.extension" */
        $target_path = $target_path.basename($file['name']);

        if(!move_uploaded_file($file['tmp_name'], $target_path)) {
            echo "There was an error uploading the file, please try again!";
        }

        return $target_path;
    }

    static public function emptyDir($path) {
        // INITIALIZE THE DEBUG STRING
        $debugStr = '';
        $debugStr .= "Deleting Contents Of: $path<br /><br />";

        // PARSE THE FOLDER
        if ($handle = opendir($path)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
        // IF IT"S A FILE THEN DELETE IT
                    if(is_file($path."/".$file)) {
                        if(unlink($path."/".$file)) {
                            $debugStr .= "Deleted File: ".$file."<br />";
                        }
                    } else {
        // IT IS A DIRECTORY
        // CRAWL THROUGH THE DIRECTORY AND DELETE IT'S CONTENTS
                        if($handle2 = opendir($path."/".$file)) {
                            while (false !== ($file2 = readdir($handle2))) {
                                if ($file2 != "." && $file2 != "..") {
                                    if(unlink($path."/".$file."/".$file2)) {
                                        $debugStr .= "Deleted File: $file/$file2<br />";
                                    }
                                }
                            }
                        }
                        if(rmdir($path."/".$file)) {
                            $debugStr .= "Directory: ".$file."<br />";
                        }
                    }
                }
            }
        }
        return $debugStr;
    }
}
